<?php

namespace app\api\controller\v1;

use think\admin\Controller;
use Yansongda\Pay\Pay as SPay;

class Pay extends Controller
{


    /**
     * 支付提交
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function orderPay()
    {
        $top_id = $this->request->get('top_id', '', 'int');
        if (empty($top_id)) $this->error('缺少必要参数');
        $user_id = get_user_id();
        //$user_id = 4;
        if (empty($user_id)) $this->error('您未登录，请先登录');
        $top_order_info = getOrderInfo(['top_id' => $top_id, 'user_id' => $user_id]);
        if (empty($top_order_info)) $this->error('操作有误，未找到该订单');
        if ($top_order_info['pay_status'] != 0) $this->error('该订单不是待付款订单');
        $user_info = session('user');
        //$user_info = get_user_info(['id' => $user_id]);
        $config_biz = [
            'out_trade_no' => $top_order_info['top_no'],                                   // 订单号
            //'total_fee' => $top_order_info['real_pay'] * 100,                            // 订单金额，单位：分
            'total_fee' => 1,                                                              // 订单金额，单位：分
            'body' => '安尚米啦 线上消费',                                                   // 订单描述
            'openid' => $user_info['openid'],
            //'time_expire' => date('YmdHis', (strtotime($top_order_info['add_time']) + 3600)),
            //'attach' => json_encode(['pay_type' => $type]),
            'detail' => "消费明细：拼团消费"
        ];
        $wx_pay_config = config('base.wx_pay_config');
        //self::$wx_pay_config['notify_url'] = "http://{$_SERVER['SERVER_NAME']}/" . self::$version . "Pay/orderPayNotify";
        $wx_pay_config['notify_url'] = "http://{$_SERVER['SERVER_NAME']}/index/index/orderPayNotify";
        try {
            $result = SPay::wechat($wx_pay_config)->mp($config_biz);
            $result = json_decode(json_encode($result), true);
            $this->assign('pay_data', $result);
            $this->assign('top_id', $top_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->fetch();
    }

}